$(function(){
   $(".del").hide();
   $("#btnsave").attr("readonly","readonly")
    $(".btnaddoption").click(function (){
        let new_option=$("\t<p><input type=\"text\" class=\"input-text\" name=\"options\" value=\"\"/><a class=\"del\" href=\"javascript:;\" >删除</a></p>");
        $("#voteoptions").append(new_option);
        if($("input[name=options]").length>2){
            $(".del").show();
        }
    });
    $("#voteoptions").on("click"," .del",function (){
        $(this).parent().remove();
        if($("input[name=options]").length>2)
            $(".del").show();
        else
            $(".del").hide();
    });
    $("form").submit(function (){
        let issubmit=true;
        $("input[name=options]").each(function (i,v){
            $("input[name=options]").each(function (index,value){
                    if(index>i){
                            if($(v).val()==$(value).val()&&$(value).val()!=""){
                                $(v).css("color","red");
                                $(value).css("color","red");
                                $(value).focus();
                                issubmit=false;
                                return false;
                            }
                            $("#errorMsg").html("");
                    }
            });
        });
        if(!issubmit)
            $("#errorMsg").html("<span style='color: red'>选项重复</span>");
        else
            $("#errorMsg").html("");
        $("#voteoptions").on("blur"," input[name=options]",function (){
            let that=$(this);
            $("#errorMsg").html("")
            $("input[name=options]").each(function (i,v){
                $(v).css("color","");
                if(that.parent().index()!=i){
                    if(that.val()==$(v).val()&&$(v).val()!=""){
                        $(v).css("color","red");
                        that.css("color","red");
                        that.focus();
                        $("#errorMsg").html("<span style='color: red'>选项重复</span>");
                        return false;
                    }
                }
            });
        });         return issubmit;
    });


});